<?php
/**
 * 文章内容
 * */
namespace app\admin\controller;
use app\admin\controller;
use app\admin\model\Site;

class Article extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();

        // 载入模型
        $this->model = Model('article');
        $this->db = db('article');
    }
    public function index()
    {
        $ser['pid'] = input('pid');
        $ser['keyword'] = trim(input('keyword'));
        $ser['is_status'] = input('is_status');


        // where条件
        $where = '';
        if ($ser['pid'])   $where['pid'] = $ser['pid'];
        if ($ser['keyword'])  $where['title'] = ['like','%'.$ser['keyword'].'%'];
        if ($ser['is_status'])   $where['is_status'] = $ser['is_status'];
        $where['is_delete'] = 1;

        // 带参查询
        $query = ['query'=>
            [
                 'keyword'=>$ser['keyword'],
                 'is_status'=>$ser['is_status'],
                 'pid'=>$ser['pid']
            ]
        ];

        $res= $this->db
            ->where($where)
            ->order('id desc')
            ->paginate(config('paginate.list_rows'),false,$query);
        // 获取分页显示
        $page = $res->render();
        // 格式化数据
        $list = $res->toArray()['data'];
        foreach($list as $k=>$v){
            $pid_name = Model('category')->info($v['pid']);
            $list[$k]['pid_name'] = $pid_name['name'];
         }

        //print_r($list);
        $this->assign('list', $list);
        $this->assign('page', $page);

        // 文章opt格式
        $ser['opt'] = $this->model->categoryOpt($ser['pid']);
        // 审核状态
        $ser['status'] = $this->isCheck($ser['is_status']);

        $this->assign('res',$ser);
        return $this->fetch();
    }

    // 添加内容
    public function add(){
        // 文章opt格式
        $res['opt'] = $this->model->categoryOpt();
        // 审核装填
        $res['is_check'] = $this->isCheck(1);
        // 新增时间
        $res['add_time'] = date('Y-m-d H:i:s',time());
        // 推荐
        $res['recommend'] = $this->model->recommendData();
        // 文章标签
        $res['tag'] = $this->model->tagData();

        $info['thumb'] = '';
        $this->assign('info',$info);

        $this->assign('res',$res);
        return $this->fetch();
    }
    public function add_ok(){

        $tag = input('tag/a');
        if ($tag) $tag = implode(',',input('tag/a'));

        //相册集

        // 相册集
        $up_thumb = input('up-thumb/a');
        $up_title = input('up-title/a');
        $up_top= input('up-top/d');

        if($up_thumb){
            foreach($up_thumb as $k=>$v){
                $thumb[$k]['src'] =  $v;
                $thumb[$k]['title'] =  $up_title[$k];

                if($k == $up_top){
                    $top = 1;
                }else{
                    $top = 0;
                }
                $thumb[$k]['top'] =  $top;
            }
            $json_thumb = json_encode($thumb,true);
        }else{
            $json_thumb='';
        }

        $data['pid']                    = input('post.pid');
        $data['title']                   = input('post.title');
        $data['keyword']            = input('post.keyword');
        $data['is_top']               = input('post.is_top',null);
        $data['is_new']              = input('post.is_new',null);
        $data['is_hot']                = input('post.is_hot',null);
        $data['desc']                  = input('post.desc');
        $data['tag']                   = $tag;
        $data['content']             = input('post.content');
        $data['is_status']          =  input('post.is_status');
        $data['hit']                   = 10;
        $data['add_time']           = strtotime(input('post.add_time'));
        $data['is_delete']          =  1; 
        $data['thumb'] = $json_thumb;

        $res = $this->model->articleInsert($data);
        $this->is_success($res);

    }

    // 修改内容
    public function edit(){
        $id = input('id');

        // 读取数据信息
        $info = $this->model->articleInfo($id);


        //格式化信息
        $info['format_add_time'] = date('Y-m-d H:i:s',$info['add_time']);
        $info['thumb_arr'] = json_decode($info['thumb'],true);
        //print_r($info['thumb_arr']);


        // 文章opt格式
        $res['opt']                      = $this->model->categoryOpt($info['pid']);
        // 审核装填
        $res['is_check']               = $this->isCheck($info['is_status']);
        // 新增时间
        $res['add_time']               =  date('Y-m-d H:i:s',time());
        // 推荐
        $res['recommend']            = $this->model->recommendData();
        // 文章标签
        $res['tag']                         = $this->model->tagData();

        $this->assign('res',$res);
        $this->assign('info',$info);
        return $this->fetch();
        
    }

    public function edit_ok(){
        $id = input('id');
        $tag = input('tag/a');
        if($tag){
           $tag =  implode(',',$tag);
        }

        // 相册集
        $up_thumb = input('up-thumb/a');
        $up_title = input('up-title/a');
        $up_top= input('up-top/d');

        if($up_thumb){
            foreach($up_thumb as $k=>$v){
                $thumb[$k]['src'] =  $v;
                $thumb[$k]['title'] =  $up_title[$k];

                if($k == $up_top){
                    $top = 1;
                }else{
                    $top = 0;
                }
                $thumb[$k]['top'] =  $top;
            }
            $json_thumb = json_encode($thumb,true);
        }else{
            $json_thumb='';
        }


        //print_r(json_encode($thumb));exit;

        $data['pid'] = input('pid');
        $data['title'] = input('title');
        $data['keyword'] = input('title');
        $data['desc'] = input('desc');
        $data['content'] = input('content');
        $data['update_time'] = time();
        $data['is_status'] = input('is_status');
        $data['tag'] = $tag;
        $data['is_top'] = input('is_top');
        $data['is_new'] = input('is_new');
        $data['is_hot'] = input('is_hot');
        $data['thumb'] = $json_thumb;

        $res = $this->model->articleUpdate($id,$data);
        $this->is_success($res);

    }

    /**
     * 回收站列表
     * */
    public function backlist(){
        $ser['pid'] = input('pid');
        $ser['keyword'] = trim(input('keyword'));
        $ser['is_status'] = input('is_status');


        // where条件
        $where = '';
        if ($ser['pid'])   $where['pid'] = $ser['pid'];
        if ($ser['keyword'])  $where['title'] = ['like','%'.$ser['keyword'].'%'];
        if ($ser['is_status'])   $where['is_status'] = $ser['is_status'];
        $where['is_delete'] = 2;

        // 带参查询
        $query = ['query'=>
            [
                'keyword'=>$ser['keyword'],
                'is_status'=>$ser['is_status'],
                'pid'=>$ser['pid']
            ]
        ];

        $res= $this->db
            ->where($where)
            ->order('id desc')
            ->paginate(config('paginate.list_rows'),false,$query);
        // 获取分页显示
        $page = $res->render();
        // 格式化数据
        $list = $res->toArray()['data'];
        foreach($list as $k=>$v){
            $pid_name = Model('category')->info($v['pid']);
            $list[$k]['pid_name'] = $pid_name['name'];
        }

        //print_r($list);
        $this->assign('list', $list);
        $this->assign('page', $page);

        // 文章opt格式
        $ser['opt'] = $this->model->categoryOpt($ser['pid']);
        // 审核状态
        $ser['status'] = $this->isCheck($ser['is_status']);

        $this->assign('res',$ser);
        return $this->fetch();
    }


    // 文章加入回收站
    public function back(){
        $id  = input('id',null,'intval');
        $res= $this->model->articleBack($id);
        $this->is_success($res);

    }

    //文章还原
    public function goback(){
        $id  = input('id',null,'intval');
        $res= $this->model->articleBack($id,1);
        $this->is_success($res);
    }

    // 文章 物理删除
    public function delete(){
        $id = input('id/d');
        $res = $this->model->articleDelete($id);
        $this->is_success($res);
    }




}
